package com.xinchuang.pojoUtil;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.xinchuang.common.ResultEnum;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.object.TargetAdd;
import com.xinchuang.entity.targetSystem.object.TargetSubAdd;
import com.xinchuang.entity.targetSystem.system.CommonAttribute;
import com.xinchuang.entity.targetSystem.system.PostureAttribute;
import com.xinchuang.entity.targetSystem.system.SpecialAttribute;
import com.xinchuang.entity.targetSystem.target.*;
import com.xinchuang.util.FileElseUtil;
import com.xinchuang.utils.ObjectUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/2/22 10:55
 */
public class TargetUtil {

    private static String RP = "[";

    /**
     * 目标基础参数封装给
     *
     * @param targetData
     * @param targetId
     * @param targetAdd
     * @param userId
     * @param msgType
     * @return
     */
    public static List<TargetMsgTable> getTargetMsg(TargetData targetData, String targetId, TargetAdd targetAdd, String userId, int msgType) {
        //基础参数
        targetData.setTargetId(targetId);
        //详细参数
        List<TargetMsgTable> msgList = new ArrayList<>();
        TargetMsgTable targetMsg;
        for (com.xinchuang.sqlite.entity.TargetData data : targetAdd.getTargetList()) {
            //单条属性值封装
            targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetId);
            targetMsg.setMsgType(msgType);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId(data.getKey());
            targetMsg.setKeyName(data.getKeyName());
            targetMsg.setKeyType(data.getKeyType());
            targetMsg.setKeyValue(data.getKeyValue());
            targetMsg.setRemark(data.getRemark());
            msgList.add(targetMsg);
            if (1 == msgType) {
                switch (data.getKey()) {
                    case "targetName":
                        targetData.setTargetName(data.getKeyValue());
                        break;
                    case "targetClassify":
                        targetData.setTargetClassify(data.getKeyValue());
                        targetData.setType(TargetUtil.getParamListOrString(data.getKeyValue()));
                        break;
                    case "grade":
                        targetData.setGrade(data.getKeyValue());
                        break;
                    case "country":
                        if (data.getKeyValue().contains("[")) {
                            List list = FileElseUtil.getStringTransitionList(data.getKeyValue());
                            if (CollectionUtil.isNotEmpty(list) && list.size() == 1) {
                                targetData.setCountry(list.get(0).toString());
                            } else {
                                targetData.setCountry(data.getKeyValue());
                            }
                        } else {
                            targetData.setCountry(data.getKeyValue());
                        }
                        break;
                    case "position":
                        List list = FileElseUtil.getStringTransitionList(data.getKeyValue());
                        if (CollectionUtil.isNotEmpty(list)) {
                            targetData.setLon(FileElseUtil.getObjectToDouble(list.get(0)));
                            targetData.setLat(FileElseUtil.getObjectToDouble(list.get(1)));
                            targetData.setHeight(FileElseUtil.getObjectToDouble(list.get(2)));
                        }
                        break;
                    case "riskLevel":
                        targetData.setRisk(data.getKeyValue());
                        break;
                    default:
                        break;
                }
            }
        }
        targetData.setDeleteSign(1);
        return msgList;
    }

    public static List<TargetMsgTable> getTargetElseMessage(TargetAdd targetAdd, List<TargetMsgTable> targetMsgTables, String userId, int msgType) {
        //过滤入参中key
        Set<String> keySet = targetAdd.getTargetList().stream().map(com.xinchuang.sqlite.entity.TargetData::getKey).collect(Collectors.toSet());
        //判断旧数据中的keyId相同的值
        List<TargetMsgTable> msgTables = targetMsgTables.stream().filter(tables -> keySet.contains(tables.getKeyId())).collect(Collectors.toList());
        //封装修改的方法
        List<TargetMsgTable> msgList = new ArrayList<>();
        TargetMsgTable targetMsg;
        for (com.xinchuang.sqlite.entity.TargetData data : targetAdd.getTargetList()) {
            Optional<TargetMsgTable> table = msgTables.stream().filter(targetMsgTable -> targetMsgTable.getKeyId().equals(data.getKey())).findFirst();
            if (table.isPresent()) {
                targetMsg = table.get();
                targetMsg.setKeyName(data.getKeyName());
                targetMsg.setKeyType(data.getKeyType());
                targetMsg.setKeyValue(data.getKeyValue());
                targetMsg.setPerson(userId);
                msgList.add(targetMsg);
                continue;
            }
            //单条属性值封装
            targetMsg = new TargetMsgTable();
            targetMsg.setRedId(targetAdd.getTargetId());
            targetMsg.setMsgType(msgType);
            targetMsg.setPerson(userId);
            targetMsg.setKeyId(data.getKey());
            targetMsg.setKeyName(data.getKeyName());
            targetMsg.setKeyType(data.getKeyType());
            targetMsg.setKeyValue(data.getKeyValue());
            targetMsg.setRemark(data.getRemark());
            msgList.add(targetMsg);
        }
        return msgList;
    }

    public static void getTargetMsg(Map<String, Object> resultMap, List<TargetMsgTable> msgTables) {
        List<Object> data = null;
        for (TargetMsgTable msgTable : msgTables) {
            data = new ArrayList<>();
            data.add(msgTable.getKeyValue());
            data.add(msgTable.getRemark());
            resultMap.put(msgTable.getKeyId(), data);
        }
    }

    /**
     * 判断json数据是否为列表，是返回列表的最后一个值，不是返回该值
     *
     * @param msg
     * @return
     */
    public static String getParamListOrString(String msg) {
        if (StringUtils.isBlank(msg)) {
            return "";
        }
        if (msg.contains(RP)) {
            JSONArray array = JSONArray.parseArray(msg);
            return array.getString(array.size() - 1);
        } else {
            return msg;
        }
    }

    public static String getObjectListOrString(Object msg) {
        if (ObjectUtil.isEmpty(msg)) {
            return "";
        }
        if (msg.toString().contains(RP)) {
            JSONArray array = JSONArray.parseArray(msg.toString());
            return array.getString(array.size() - 1);
        } else {
            return msg.toString();
        }
    }

    /**
     * 子目标参数的封装
     *
     * @param subId
     * @param list
     * @param userId
     * @param msgType
     * @return
     */
    public static List<TargetSubMsgTable> getSubMsg(String subId, List<com.xinchuang.sqlite.entity.TargetData> list, String userId, int msgType) {
        //详细参数
        List<TargetSubMsgTable> msgList = new ArrayList<>();
        TargetSubMsgTable targetSubMsg;
        for (com.xinchuang.sqlite.entity.TargetData data : list) {
            //单条属性值封装
            targetSubMsg = new TargetSubMsgTable();
            targetSubMsg.setSubId(subId);
            targetSubMsg.setMsgType(msgType);
            targetSubMsg.setPerson(userId);
            targetSubMsg.setKeyId(data.getKey());
            targetSubMsg.setKeyName(data.getKeyName());
            targetSubMsg.setKeyType(data.getKeyType());
            targetSubMsg.setKeyValue(data.getKeyValue());
            targetSubMsg.setRemark(data.getRemark());
            msgList.add(targetSubMsg);
        }
        return msgList;
    }

    public static List<TargetSubMsgTable> getSubElseMessage(String subId, List<com.xinchuang.sqlite.entity.TargetData> targetData, List<TargetSubMsgTable> targetMsgTables, String userId, int msgType) {
        //过滤入参中key
        Set<String> keySet = targetData.stream().map(com.xinchuang.sqlite.entity.TargetData::getKey).collect(Collectors.toSet());
        //判断旧数据中的keyId相同的值
        List<TargetSubMsgTable> msgTables = targetMsgTables.stream().filter(tables -> keySet.contains(tables.getKeyId())).collect(Collectors.toList());
        //封装修改的方法
        List<TargetSubMsgTable> msgList = new ArrayList<>();
        TargetSubMsgTable subMsg;
        for (com.xinchuang.sqlite.entity.TargetData data : targetData) {
            Optional<TargetSubMsgTable> table = msgTables.stream().filter(subMsgTable -> subMsgTable.getKeyId().equals(data.getKey())).findFirst();
            if (table.isPresent()) {
                subMsg = table.get();
                subMsg.setKeyName(data.getKeyName());
                subMsg.setKeyType(data.getKeyType());
                subMsg.setKeyValue(data.getKeyValue());
                subMsg.setPerson(userId);
                msgList.add(subMsg);
                continue;
            }
            //单条属性值封装
            subMsg = new TargetSubMsgTable();
            subMsg.setSubId(subId);
            subMsg.setMsgType(msgType);
            subMsg.setPerson(userId);
            subMsg.setKeyId(data.getKey());
            subMsg.setKeyName(data.getKeyName());
            subMsg.setKeyType(data.getKeyType());
            subMsg.setKeyValue(data.getKeyValue());
            subMsg.setRemark(data.getRemark());
            msgList.add(subMsg);
        }
        return msgList;
    }

    public static void getSubMsg(Map<String, Object> resultMap, List<TargetSubMsgTable> msgTables) {
        List<Object> data = null;
        for (TargetSubMsgTable msgTable : msgTables) {
            data = new ArrayList<>();
            data.add(msgTable.getKeyValue());
            data.add(msgTable.getRemark());
            resultMap.put(msgTable.getKeyId(), data);
        }
    }

    /**
     * 态势参数的封装
     *
     * @param postureId
     * @param list
     * @param userId
     * @param msgType
     * @return
     */
    public static List<TargetPostureMsgTable> getPostureMsg(String postureId, List<com.xinchuang.sqlite.entity.TargetData> list, String userId, int msgType) {
        //详细参数
        List<TargetPostureMsgTable> msgList = new ArrayList<>();
        TargetPostureMsgTable targetPostureMsg;
        for (com.xinchuang.sqlite.entity.TargetData data : list) {
            //单条属性值封装
            targetPostureMsg = new TargetPostureMsgTable();
            targetPostureMsg.setPostureId(postureId);
            targetPostureMsg.setMsgType(msgType);
            targetPostureMsg.setPerson(userId);
            targetPostureMsg.setKeyId(data.getKey());
            targetPostureMsg.setKeyName(data.getKeyName());
            targetPostureMsg.setKeyType(data.getKeyType());
            targetPostureMsg.setKeyValue(data.getKeyValue());
            targetPostureMsg.setRemark(data.getRemark());
            msgList.add(targetPostureMsg);
        }
        return msgList;
    }

    public static void getPostureMsg(Map<String, Object> resultMap, List<TargetPostureMsgTable> msgTables) {
        List<Object> data = null;
        for (TargetPostureMsgTable msgTable : msgTables) {
            data = new ArrayList<>();
            data.add(msgTable.getKeyValue());
            data.add(msgTable.getRemark());
            resultMap.put(msgTable.getKeyId(), data);
        }
    }

    /**
     * 共有属性值封装
     *
     * @param resultMap
     * @param commonAttributes
     */
    public static void getCommonAttributesList(Map<String, Object> resultMap, List<CommonAttribute> commonAttributes) {
        if (CollectionUtil.isNotEmpty(commonAttributes)) {
            List<Object> data = null;
            for (CommonAttribute commonAttribute : commonAttributes) {
                data = new ArrayList<>();
                data.add("");
                data.add("");
                resultMap.put(commonAttribute.getKey(), data);
            }
        }
    }

    /**
     * 私有属性值封装
     *
     * @param resultMap
     * @param specialAttributes
     */
    public static void getSpecialAttributesList(Map<String, Object> resultMap, List<SpecialAttribute> specialAttributes) {
        if (CollectionUtil.isNotEmpty(specialAttributes)) {
            List<Object> data = null;
            for (SpecialAttribute specialAttribute : specialAttributes) {
                data = new ArrayList<>();
                data.add("");
                data.add("");
                resultMap.put(specialAttribute.getKey(), data);
            }
        }
    }

    /**
     * 态势属性值封装
     *
     * @param resultMap
     * @param postureAttributes
     */
    public static void getPostureAttributesList(Map<String, Object> resultMap, List<PostureAttribute> postureAttributes) {
        if (CollectionUtil.isNotEmpty(postureAttributes)) {
            List<Object> data = null;
            for (PostureAttribute postureAttribute : postureAttributes) {
                data = new ArrayList<>();
                data.add("");
                data.add("");
                resultMap.put(postureAttribute.getKeyValue(), data);
            }
        }
    }

    /**
     * 将目标的属性参数封装为map集合
     *
     * @param list
     * @return
     */
    public static Map<String, Object> getTargetMsgByMap(List<TargetMsgTable> list) {
        Map<String, Object> map = new HashMap<>();
        for (TargetMsgTable msgTable : list) {
            map.put(msgTable.getKeyId(), msgTable.getKeyValue());
        }
        return map;
    }

    /**
     * 将子目标的属性参数封装为map集合
     *
     * @param list
     * @return
     */
    public static Map<String, Object> getSubMsgByMap(List<TargetSubMsgTable> list) {
        Map<String, Object> map = new HashMap<>();
        for (TargetSubMsgTable msgTable : list) {
            map.put(msgTable.getKeyId(), msgTable.getKeyValue());
        }
        return map;
    }

    /**
     * 将态势的属性参数封装为map集合
     *
     * @param list
     * @return
     */
    public static Map<String, Object> getPostureMsgByMap(List<TargetPostureMsgTable> list) {
        Map<String, Object> map = new HashMap<>();
        for (TargetPostureMsgTable msgTable : list) {
            map.put(msgTable.getKeyId(), msgTable.getKeyValue());
        }
        return map;
    }

}
